package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.mwsreportbean.MwsLongTermStorageFeeCharges;
import com.yunchang.springboot.models.mwsreportbean.MwsLongTermStorageFeeChargesDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:47
 * @description:
 */
@Component
public class LongTermStorageFeeChargesDataDispose extends ReportDataDisposeParent implements IReportDataDispose {
    /**
     * 长期仓储费 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<Object> insertList = new ArrayList<>(lists.size());
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        if (null != area && area.equals(ESPECIALLY_AREA) && !shopBrandName.equals(SPECIAL_SHOP_NAME)) {
//            //获取数据库里面最新的日期
//            String latestDay = mwsLongTermStorageFeeChargesMapper.getLatestDay(sellerId, area);
            int snapshotDateIndex = Integer.valueOf(String.valueOf(titleMap.get("Date")));
//            if (!StringUtil.isBlank(latestDay)) {
//                //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//                if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(lists.get(1).get(snapshotDateIndex))) >= 0) {
//                    return null;
//                }
//            }
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("Merchant SKU")));
            int fnSkuIndex = Integer.valueOf(String.valueOf(titleMap.get("FNSKU")));
            int asinIndex = Integer.valueOf(String.valueOf(titleMap.get("ASIN")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("Title")));
            int conditionIndex = Integer.valueOf(String.valueOf(titleMap.get("Condition")));
            int qtyCharged12MoLongTermStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("qty-charged-12-mo-long-term-storage-fee")));
            int perUnitVolumeIndex = Integer.valueOf(String.valueOf(titleMap.get("Unit Volume")));
            int currencyIndex = Integer.valueOf(String.valueOf(titleMap.get("Currency")));
            int w12MoLongTermsStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("12-mo-long-term-storage-fee")));
            int qtyCharged6MoLongTermStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("qty-charged-6-mo-long-term-storage-fee")));
            int w6MoLongTermsStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("6-mo-long-term-storage-fee")));
            int volumeUnitIndex = Integer.valueOf(String.valueOf(titleMap.get("volume-unit")));
            int countryIndex = Integer.valueOf(String.valueOf(titleMap.get("Country")));
            int enrolledInSmallAndLightIndex = Integer.valueOf(String.valueOf(titleMap.get("enrolled-in-small-and-light")));

            for (int i = 1; i < lists.size(); i++) {
                MwsLongTermStorageFeeChargesDO mwsLongTermStorageFeeCharges = new MwsLongTermStorageFeeChargesDO();
                List<String> row = lists.get(i);
                mwsLongTermStorageFeeCharges.setSellerId(sellerId);
                mwsLongTermStorageFeeCharges.setArea(area);
                mwsLongTermStorageFeeCharges.setSnapshotDate(DateUtil.ISO8601ToGMT(row.get(snapshotDateIndex)));
                mwsLongTermStorageFeeCharges.setSku(row.get(skuIndex));
                mwsLongTermStorageFeeCharges.setFnsku(row.get(fnSkuIndex));
                mwsLongTermStorageFeeCharges.setAsin(row.get(asinIndex));
                mwsLongTermStorageFeeCharges.setProductName(row.get(productNameIndex));
                mwsLongTermStorageFeeCharges.setGoodsCondition(row.get(conditionIndex));
                mwsLongTermStorageFeeCharges.setQtyCharged12MoLongTermStorageFee(ToolUtil.valueOfInteger(row.get(qtyCharged12MoLongTermStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setPerUnitVolume(new BigDecimal(String.valueOf(row.get(perUnitVolumeIndex))));
                mwsLongTermStorageFeeCharges.setCurrency(row.get(currencyIndex));
                mwsLongTermStorageFeeCharges.setMoLongTermsStorageFee12(String.valueOf(row.get(w12MoLongTermsStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setQtyCharged6MoLongTermStorageFee(String.valueOf(row.get(qtyCharged6MoLongTermStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setMoLongTermsStorageFee6(String.valueOf(row.get(w6MoLongTermsStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setVolumeUnit(row.get(volumeUnitIndex));
                mwsLongTermStorageFeeCharges.setCountry(row.get(countryIndex));
                mwsLongTermStorageFeeCharges.setEnrolledInSmallAndLight(row.get(enrolledInSmallAndLightIndex));
                mwsLongTermStorageFeeCharges.setCreateTime(DateUtil.getDate());
                mwsLongTermStorageFeeCharges.setUpdateTime(DateUtil.getDate());
                mwsLongTermStorageFeeCharges.setFinanceStatus(1);
                insertList.add(mwsLongTermStorageFeeCharges);
            }
            return insertList;
        } else {
            //获取数据库里面最新的日期
//            String latestDay = mwsLongTermStorageFeeChargesMapper.getLatestDay(sellerId, area);
            int snapshotDateIndex = Integer.valueOf(String.valueOf(titleMap.get("snapshot-date")));
//            if (!StringUtil.isBlank(latestDay)) {
//                //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//                if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(lists.get(1).get(snapshotDateIndex))) >= 0) {
//                    return null;
//                }
//            }
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
            int fnSkuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
            int asinIndex = Integer.valueOf(String.valueOf(titleMap.get("asin")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
            int conditionIndex = Integer.valueOf(String.valueOf(titleMap.get("condition")));
            int qtyCharged12MoLongTermStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("qty-charged-12-mo-long-term-storage-fee")));
            int perUnitVolumeIndex = Integer.valueOf(String.valueOf(titleMap.get("per-unit-volume")));
            int currencyIndex = Integer.valueOf(String.valueOf(titleMap.get("currency")));
            int w12MoLongTermsStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("12-mo-long-terms-storage-fee")));
            int qtyCharged6MoLongTermStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("qty-charged-6-mo-long-term-storage-fee")));
            int w6MoLongTermsStorageFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("6-mo-long-terms-storage-fee")));
            int volumeUnitIndex = Integer.valueOf(String.valueOf(titleMap.get("volume-unit")));
            int countryIndex = Integer.valueOf(String.valueOf(titleMap.get("country")));
            int enrolledInSmallAndLightIndex = Integer.valueOf(String.valueOf(titleMap.get("enrolled-in-small-and-light")));
            for (int i = 1; i < lists.size(); i++) {
                MwsLongTermStorageFeeChargesDO mwsLongTermStorageFeeCharges = new MwsLongTermStorageFeeChargesDO();
                List<String> row = lists.get(i);
                mwsLongTermStorageFeeCharges.setSellerId(sellerId);
                mwsLongTermStorageFeeCharges.setArea(area);
                mwsLongTermStorageFeeCharges.setSnapshotDate(DateUtil.ISO8601ToGMT(row.get(snapshotDateIndex)));
                mwsLongTermStorageFeeCharges.setSku(row.get(skuIndex));
                mwsLongTermStorageFeeCharges.setFnsku(row.get(fnSkuIndex));
                mwsLongTermStorageFeeCharges.setAsin(row.get(asinIndex));
                mwsLongTermStorageFeeCharges.setProductName(row.get(productNameIndex));
                mwsLongTermStorageFeeCharges.setGoodsCondition(row.get(conditionIndex));
                mwsLongTermStorageFeeCharges.setQtyCharged12MoLongTermStorageFee(ToolUtil.valueOfInteger(row.get(qtyCharged12MoLongTermStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setPerUnitVolume(new BigDecimal(String.valueOf(row.get(perUnitVolumeIndex))));
                mwsLongTermStorageFeeCharges.setCurrency(row.get(currencyIndex));
                mwsLongTermStorageFeeCharges.setMoLongTermsStorageFee12(String.valueOf(row.get(w12MoLongTermsStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setQtyCharged6MoLongTermStorageFee(String.valueOf(row.get(qtyCharged6MoLongTermStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setMoLongTermsStorageFee6(String.valueOf(row.get(w6MoLongTermsStorageFeeIndex)));
                mwsLongTermStorageFeeCharges.setVolumeUnit(row.get(volumeUnitIndex));
                mwsLongTermStorageFeeCharges.setCountry(row.get(countryIndex));
                mwsLongTermStorageFeeCharges.setEnrolledInSmallAndLight(row.get(enrolledInSmallAndLightIndex));
                mwsLongTermStorageFeeCharges.setCreateTime(DateUtil.getDate());
                mwsLongTermStorageFeeCharges.setUpdateTime(DateUtil.getDate());
                mwsLongTermStorageFeeCharges.setFinanceStatus(1);
                insertList.add(mwsLongTermStorageFeeCharges);
            }
            return insertList;
        }
    }
}
